package com.second.service.impl;

import com.second.consts.CouponStatus;
import com.second.consts.PersonalConst;
import com.second.mapper.FCommentMapper;
import com.second.mapper.FCommentZanMapper;
import com.second.mapper.IFCouponMapper;
import com.second.model.pojo.FCoupon;
import com.second.service.IFCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FCouponServiceImpl implements IFCouponService {

    @Autowired
    private IFCouponMapper couponDao;

    @Autowired
    private FCommentZanMapper fCommentZan;

    @Autowired
    private FCommentMapper fComment;

    /**
     * 查看所有优惠券-点赞数
     * @return
     */

    @Override
    public List<FCoupon> findAllZan(Integer userId) {
        if(fCommentZan.countByUserIdAndStatus(userId, PersonalConst.Zan)>=10){
               List<FCoupon> fcoupon = couponDao.findAllByStatusAndType(CouponStatus.Coupon_Unclaimed,1);
               Date date = new Date();
               List<FCoupon> coupon1 = new ArrayList<>();
            for (FCoupon f : fcoupon) {
                if(f.getEffectiveDays().getTime()>date.getTime()){
                    coupon1.add(f);
                }
            }

            return coupon1;
        }
        return null;
    }
    /**
     * 查看所有优惠券-评论数
     * @return
     */
    @Override
    public List<FCoupon> findAllComment(Integer userId) {
        if(fComment.countByUserId(userId)>=5){
            List<FCoupon> fcoupon = couponDao.findAllByStatusAndType(CouponStatus.Coupon_Unclaimed,2);
            Date date = new Date();
            List<FCoupon> coupon1 = new ArrayList<>();
            for (FCoupon f : fcoupon) {
                if(f.getEffectiveDays().getTime()>date.getTime()){
                    coupon1.add(f);
                }

            }

            return coupon1;
        }
        return null;
    }

}
